#-*- coding: UTF-8 -*-
'''
Created on Mar 8, 2011

@author: Juho Salmio
'''

import plistlib
import os.path


plistpath = os.path.expanduser('~/Library/Preferences/com.Growl.GrowlHelperApp.plist')

#Binary plist to xmlplist 
os.system('/usr/bin/plutil -convert xml1 '+plistpath)


#read the xmlfile
plist = plistlib.readPlist(plistpath)


logpath = ''
logpathtxt = open(os.path.expanduser('~/Library/Logs/Growl_logpath'),'w')

# If logging is already enabled we keep the current logfile in place
if plist['GrowlLoggingEnabled'] == 'True':
    logpath = plist['Custom log history 1']
    
    
else:

    plist['GrowlLoggingEnabled'] = "True"
    plist['Custom log history 1'] = os.path.expanduser('~/Library/Logs/Growl_log')
    logpath = os.path.expanduser('~/Library/Logs/Growl_log')


plist['GrowlLogType'] = "1"
#write the logfilepath to file
logpathtxt.write(logpath)
logpathtxt.close()

#Save
plistlib.writePlist(plist, plistpath)

logfile = open(os.path.expanduser('~/Library/Logs/Growl_log'),'w')
logfile.write('')
logfile.close()

#restart growl
quitGrowl = """osascript -e 'tell app "GrowlHelperApp" to quit'"""
os.system(quitGrowl)

startGrowl = """osascript -e 'tell app "GrowlHelperApp" to activate'"""
os.system(startGrowl)


